<template>
	<view class="auth">
		<view class="info">
			<view class="line imgBox">
				<view class="l">证书：</view>
				<view class="imgBox">
					<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
						:maxCount="10"></u-upload>
				</view>
			</view>
			<view class="line imgBox">
				<view class="l">经验：</view>
				<textarea v-model="experiencecontent" class="r" placeholder="请输入经验信息" />
			</view>
		
		
			
		</view>
		<view class="btn" @click="save">发布</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				experiencecontent: '',
				fileList1: [],
				status: ''
			};
		},
		onLoad() {
			this.getInfo()
		},
		methods: {
			save() {
				let images = this.fileList1.map((value) => {
					return value.fullurl
				})
				
				uni.$u.http.post('/experience', {
					type: 'update',
					experiencecontent: this.experiencecontent,
					certimages:  images.join(',')
				}).then(res => {
					if (res.code === 1) {
						this.$u.toast(res.msg);
					}
				}).catch((res) => {
		
				})
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				console.log(event.name)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					console.log(result)
					if (result) {
						let item = this[`fileList${event.name}`][fileListLen]
						this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
							status: 'success',
							message: '',
							url: this.oss(result),
							fullurl: result
						}))
						fileListLen++
					} else {
						this[`fileList${event.name}`].splice(fileListLen, 1)
					}
		
				}
				console.log(this[`fileList${event.name}`])
			},
			uploadFilePromise(url) {
				let that = this
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: that.Upload.uploadurl, // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							type: 'image',
							token: uni.getStorageSync('token')
						},
						success: (res) => {
							let data = JSON.parse(res.data)
							if (res.statusCode === 200 && data.code === 1 && data.data) {
								resolve(data.data.url)
							} else {
								resolve()
							}
						}
					});
				})
			},
			getInfo() {
				uni.$u.http.post('/experience',{
					type: ''
				}).then(res => {
					if (res.code === 1) {
						this.experiencecontent = res.data.experiencecontent
						if(res.data.certimages && res.data.certimages.length> 0){
							this.fileList1 = res.data.certimages.map((value) => {
								return 	{
									fullurl: value,
									url: this.oss(value),
								}
							})
						}
						// if(res.data) {
						// 	this.info.name = res.data.name
						// 	this.info.mobile = res.data.mobile
						// 	this.info.zimage = res.data.zimage
						// 	this.info.fimage = res.data.fimage
						// 	this.status = res.data.status
						// 	this.fileList1 = [
						// 		{
						// 			fullurl: res.data.zimage,
						// 			url: this.oss(res.data.zimage),
						// 		}
						// 	]
						// 	this.fileList2 = [
						// 		{
						// 			fullurl: res.data.fimage,
						// 			url: this.oss(res.data.fimage)
						// 		}
						// 	]
						// 	console.log(this.fileList1)
						// 		console.log(this.fileList2)
						// } else {
						// 	this.status = ''
						// }
					}
				}).catch((res) => {
					
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	page{
		background: #F6F6F6;
	}
.auth{
	background: linear-gradient(to bottom, #FFC141, #F6F6F6);
	background-size: 750rpx 500rpx;
	background-repeat: no-repeat;
	padding-top: 50rpx;
	.btn{
		background: linear-gradient(to right, #FFDF41, #FFC141);
		height: 88rpx;
		border: 44rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		margin: 50rpx auto 0;
		width: 590rpx;
		border-radius: 44rpx;
	}
	.agree{
		display: flex;
		align-items: center;
		padding: 30rpx 20rpx 20rpx;
		.circle{
			width: 22rpx;
			height: 22rpx;
			box-sizing: border-box;
			border-radius: 50%;
			border: 1px solid #999999;
			margin-right: 10rpx;
		}
		.select{
			width: 22rpx;
			height: 22rpx;
		}
		.other{
			font-size: 22rpx;
			color: #999999;
		}
		.rule{
			font-size: 22rpx;
			color: #FF8007;
		}
	}
	.notice{
		font-size: 22rpx;
		color: #FF8007;
		padding: 0 20rpx;
	}
	.info{	
		background: #FFFFFF;
		margin: 0 20rpx;
		border-radius: 20rpx;
		padding: 0 20rpx 20rpx;
		
		.line{
			height: 100rpx;
			display: flex;
			align-items: center;
			border-bottom: 1px solid #F6F6F6;
			
			.l{
				color: #575859;
				font-size: 32rpx;
				width: 180rpx;
				text-align: right;
				flex-shrink: 0;
			}
			.r{
				flex: 1;
			}
		}
		.imgBox{
			height: auto;
			align-items: flex-start;
			padding: 20rpx 0;
			justify-content: flex-start;
			position: relative;
			.address{
				position: absolute;
				width: 30rpx;
				height: 40rpx;
				right: 30rpx;
				bottom: 30rpx;
			}
		}
		.box{
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			height: 170rpx;
			width: 170rpx;
			border: 1px solid #FFCE54;
			border-radius: 20rpx;
			.icon{
				width: 51rpx;
				height: 49rpx;
			}
			.word{
				color: #FF8007;
				font-size: 30rpx;
				margin-top: 20rpx;
			}
		}
	}
}
</style>
